import type { ProcessingTask, ResultPanel } from '../types/home'

// 格式化日期
export const formatDate = (date: Date | string) => {
  return new Date(date).toLocaleDateString('zh-CN')
}

// 格式化时间
export const formatTime = (date: Date | string) => {
  return new Date(date).toLocaleTimeString('zh-CN', { 
    hour: '2-digit', 
    minute: '2-digit' 
  })
}

// 获取对话图标
export const getConversationIcon = (type: string) => {
  const icons = {
    'knowledge': 'fas fa-book-open text-blue-500',
    'data': 'fas fa-chart-line text-indigo-500',
    'image': 'fas fa-image text-green-500',
    'code': 'fas fa-code text-purple-500'
  }
  return icons[type as keyof typeof icons] || 'fas fa-comments text-slate-500'
}

// 获取对话类型标签
export const getConversationTypeLabel = (type: string) => {
  const labels = {
    'knowledge': '知识问答',
    'data': '数据分析',
    'image': '图像识别',
    'code': '代码分析'
  }
  return labels[type as keyof typeof labels] || '普通对话'
}

// 获取任务图标
export const getTaskIcon = (task: ProcessingTask) => {
  if (task.status === 'completed') {
    return 'fas fa-check'
  }
  return task.icon
}

// 获取任务图标样式
export const getTaskIconClasses = (task: ProcessingTask) => {
  const classes: Record<string, string> = {
    'pending': 'bg-slate-300',
    'processing': 'bg-blue-500',
    'completed': 'bg-green-500'
  }
  return classes[task.status] || 'bg-slate-300'
}

// 获取任务状态
export const getTaskStatus = (task: ProcessingTask) => {
  const statuses: Record<string, string> = {
    'pending': '等待中',
    'processing': '处理中',
    'completed': '已完成'
  }
  return statuses[task.status] || ''
}

// 获取任务状态样式
export const getTaskStatusClasses = (task: ProcessingTask) => {
  const classes: Record<string, string> = {
    'pending': 'text-slate-500',
    'processing': 'text-blue-600',
    'completed': 'text-green-600'
  }
  return classes[task.status] || 'text-slate-500'
}

// 获取任务样式类
export const getTaskClasses = (task: ProcessingTask) => {
  const base = 'border-2 bg-white cursor-pointer'
  let statusClass = ''
  
  switch (task.status) {
    case 'completed':
      statusClass = 'border-green-200 completed-task'
      break
    case 'processing':
      statusClass = 'border-blue-200 active'
      break
    default:
      statusClass = 'border-slate-200'
  }
  
  // 如果任务被激活，添加active状态
  if (task.active) {
    statusClass += ' active'
  }
  
  return `${base} ${statusClass}`
}

// 获取任务详情项
export const getTaskDetailItems = (task: ProcessingTask) => {
  return task.details.items
}

// 获取任务详情标题
export const getTaskDetailTitle = (task: ProcessingTask) => {
  return task.details.title
}

// 获取面板标题
export const getPanelTitle = (step: number) => {
  const titles = {
    1: '问题解析',
    2: '知识检索',
    3: '内容生成',
    4: '结果优化'
  }
  return titles[step as keyof typeof titles] || '处理步骤'
}

// 获取面板图标
export const getPanelIcon = (step: number) => {
  const icons = {
    1: 'fas fa-search',
    2: 'fas fa-book',
    3: 'fas fa-magic',
    4: 'fas fa-check-circle'
  }
  return icons[step as keyof typeof icons] || 'fas fa-cog'
}

// 获取面板图标背景
export const getPanelIconBg = (step: number) => {
  const backgrounds = {
    1: 'bg-blue-500',
    2: 'bg-indigo-500',
    3: 'bg-purple-500',
    4: 'bg-green-500'
  }
  return backgrounds[step as keyof typeof backgrounds] || 'bg-slate-500'
}

// 获取面板步骤标记
export const getPanelStepBadge = (step: number) => {
  const badges = {
    1: 'bg-blue-100 text-blue-600',
    2: 'bg-indigo-100 text-indigo-600',
    3: 'bg-purple-100 text-purple-600',
    4: 'bg-green-100 text-green-600'
  }
  return badges[step as keyof typeof badges] || 'bg-slate-100 text-slate-600'
}

// 获取面板内容
export const getPanelContent = (step: number, type: string) => {
  const contents: Record<number, string> = {
    1: `
      <div class="space-y-3">
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 意图识别：问答类问题</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 关键词提取：${type === 'knowledge' ? '技术文档' : type === 'data' ? '数据分析' : type === 'image' ? '图像处理' : '代码开发'}</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 难度评估：中等</span>
        </div>
      </div>
    `,
    2: `
      <div class="space-y-3">
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 检索范围：个人知识库</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 匹配文档：3篇相关文档</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 相关度评分：85%</span>
        </div>
      </div>
    `,
    3: `
      <div class="space-y-3">
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 生成策略：综合多文档信息</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 内容组织：结构化回答</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 质量检查：通过</span>
        </div>
      </div>
    `,
    4: `
      <div class="space-y-3">
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 格式优化：段落清晰</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 语言润色：自然流畅</span>
        </div>
        <div class="flex items-center space-x-2">
          <i class="fas fa-check-circle text-green-500"></i>
          <span>✓ 输出格式：结构化回答</span>
        </div>
      </div>
    `
  }
  
  return contents[step] || '<div>处理完成</div>'
}

// 滚动到元素
export const scrollToElement = (selector: string, options: ScrollIntoViewOptions = { behavior: 'smooth', block: 'center' }) => {
  setTimeout(() => {
    const element = document.querySelector(selector)
    if (element) {
      element.scrollIntoView(options)
    }
  }, 100)
}